package com.meihua.code.thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @program: code
 * @description:
 * @author: meihua
 * @created: 2021/08/04 16:54
 */
public class AQS {


   private static final ThreadPoolExecutor executor = new ThreadPoolExecutor(100, 100, 100L, TimeUnit.SECONDS, new ArrayBlockingQueue<>(100));
   private static final Semaphore SEMAPHORE = new Semaphore(1);


    public static void main(String[] args) {
        Thread thread = new Thread(() -> {
             while (true){
                 try {
                     Thread.sleep(100);
                     SEMAPHORE.release();
                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 }
             }
        });
        thread.setDaemon(true);
        thread.start();
        for (int i=0;i<100;i++){

            executor.execute(()->{
                try {
                    SEMAPHORE.acquire();
                    System.out.println(System.currentTimeMillis()+Thread.currentThread().getName()+"抢到了机会");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }

    }
}
